package ustc.xiaoyang.karl.linkedlist;

/**
 * 206.反转链表
 */
public class ReverseLinkedList206 {
    public static void main(String[] args) {
        ListNode first = new ListNode(1,null);
        ListNode two = new ListNode(2,first.next);
        first.next = two;
        ListNode three = new ListNode(3,first);
        ListNode four = new ListNode(4,three);
        ListNode five = new ListNode(5,four);
        printList(five);
        printList(reverseList(five));
    }
    public static ListNode reverseList(ListNode head){
        ListNode pre = null;
        ListNode cur = head;
        while(cur != null){
            ListNode tmp = cur.next;
            cur.next = pre;
            pre = cur;
            cur = tmp;
        }
        return pre;
    }
    public static void printList(ListNode head){
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }


}
